using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Drawing;

using TreeSharp;

using Styx;
using Styx.Logic;
using Styx.Logic.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Styx.Helpers;

namespace FelMaster.Classes.Mage
{
    class Arcane : TalentBase
    {
        public override string Name
        {
            get { return "Arcane V0.51"; }
        }

        public override string KeySpell
        {
            get { return "Arcane Barrage"; }
        }

        public override Composite SingleRotation
        {
            get {
                return new PrioritySelector(
                    new Decorator(x => SC.PlayerIsChanneling, new TreeSharp.Action(a => SC.Log("Channeling"))),
                    SC.UseEngineerGloves(a => true, "Engineer's glove enchant"),
                    SC.CastSpell("Evocation", a => Me.ManaPercent < 36, "Evocation"),
                    #region Burn
                    new Decorator(
                        ret => SC.SpellCooldown("Evocation") < 25,
                        new PrioritySelector(
                            SC.CastSpell("Flame Orb", a => Me.CurrentTarget.HealthPercent > 5, "Flame Orb"),
                            SC.UseBagItem("Mana Gem", a => Me.ActiveAuras.ContainsKey("Arcane Blast") && Me.ActiveAuras["Arcane Blast"].StackCount == 4, "Using Mana Gem"),
                            SC.CastSpell("Arcane Power", a => SC.PlayerHasBuff("Improved Mana Gem") || Me.CurrentTarget.HealthPercent < 5, "Arcane Power"),
                            SC.CastSpell("Mirror Image", a => SC.PlayerHasBuff("Arcane Power") && Me.CurrentTarget.HealthPercent > 5 && ((Me.CurrentTarget.Level == 87 && Me.CurrentTarget.Elite) || (Me.CurrentTarget.Level == 88)), "Mirror Image"),
                            SC.CastSpell("Presence of Mind", a => true, "Presence of Mind"),
                            #region BurnMoving
                            new Decorator(
                                ret => Me.IsMoving,
                                new PrioritySelector(
                                    SC.CastAreaSpell("Arcane Explosion", 8, false, 4, 0.0, 0.0, ret => true, "Arcane Explosion"),
                                    SC.CastSpell("Presence of Mind", a => true, "Presence of Mind"),
                                    SC.CastSpell("Arcane Blast", a => SC.PlayerHasBuff("Presence of Mind"), "Arcane Blast"),
                                    SC.CastSpell("Fire Blast", a => true, "Fire Blast"),
                                    SC.CastSpell("Ice Lance", a => true, "Ice Lance")
                                )
                            ),
                            #endregion
                            SC.CastSpell("Arcane Blast", a => true, "Arcane Blast")
                        )
                    ),
                    #endregion
                    #region LowHP
                    new Decorator(
                        ret => Me.CurrentTarget.HealthPercent < 15 && Me.ManaPercent > 15,
                        new PrioritySelector(
                            SC.CastSpell("Flame Orb", a => Me.CurrentTarget.HealthPercent > 5, "Flame Orb"),
                            SC.UseBagItem("Mana Gem", a => Me.ActiveAuras.ContainsKey("Arcane Blast") && Me.ActiveAuras["Arcane Blast"].StackCount == 4, "Using Mana Gem"),
                            SC.CastSpell("Mirror Image", a => Me.CurrentTarget.HealthPercent > 5 && ((Me.CurrentTarget.Level == 87 && Me.CurrentTarget.Elite) || (Me.CurrentTarget.Level == 88)), "Mirror Image"),
                            SC.CastSpell("Presence of Mind", a => true, "Presence of Mind"),
                            #region BurnMoving
                            new Decorator(
                                ret => Me.IsMoving,
                                new PrioritySelector(
                                    SC.CastAreaSpell("Arcane Explosion", 8, false, 4, 0.0, 0.0, ret => true, "Arcane Explosion"),
                                    SC.CastSpell("Presence of Mind", a => true, "Presence of Mind"),
                                    SC.CastSpell("Arcane Blast", a => SC.PlayerHasBuff("Presence of Mind"), "Arcane Blast"),
                                    SC.CastSpell("Fire Blast", a => true, "Fire Blast"),
                                    SC.CastSpell("Ice Lance", a => true, "Ice Lance")
                                )
                            ),
                            #endregion
                            SC.CastSpell("Arcane Blast", a => true, "Arcane Blast")
                        )
                    ),
                    #endregion
                    #region Moving
                    new Decorator(
                        ret => Me.IsMoving,
                        new PrioritySelector(
                            SC.CastAreaSpell("Arcane Explosion", 8, false, 4, 0.0, 0.0, ret => true, "Arcane Explosion"),
                            SC.CastSpell("Presence of Mind", a => true, "Presence of Mind"),
                            SC.CastSpell("Arcane Blast", a => SC.PlayerHasBuff("Presence of Mind"), "Arcane Blast"),
                            SC.CastSpell("Arcane Barrage", a => true, "Arcane Barrage"),
                            SC.CastSpell("Fire Blast", a => true, "Fire Blast"),
                            SC.CastSpell("Ice Lance", a => true, "Ice Lance")
                        )
                    ),
                    #endregion
                    #region Conserve
                    new Decorator(
                        ret => SC.SpellCooldown("Evocation") > 25,
                        new PrioritySelector(
                            SC.CastSpell("Flame Orb", a => Me.CurrentTarget.HealthPercent > 5, "Flame Orb"),
                            SC.CastAreaSpell("Arcane Explosion", 8, false, 4, 0.0, 0.0, ret => true, "Arcane Explosion"),
                            SC.CastSpell("Presence of Mind", a => true, "Presence of Mind"),
                            SC.CastSpell("Arcane Missiles", a => SC.PlayerHasBuff("Arcane Missiles!") && Me.ManaPercent < 92, "Arcane Missiles"),
                            SC.CastSpell("Arcane Barrage", a => Me.ActiveAuras.ContainsKey("Arcane Blast") && Me.ActiveAuras["Arcane Blast"].StackCount == 2 && Me.ManaPercent < 87, "Arcane Barrage"),
                            SC.CastSpell("Arcane Barrage", a => Me.ActiveAuras.ContainsKey("Arcane Blast") && Me.ActiveAuras["Arcane Blast"].StackCount == 3 && Me.ManaPercent < 90, "Arcane Barrage"),
                            SC.CastSpell("Arcane Barrage", a => Me.ActiveAuras.ContainsKey("Arcane Blast") && Me.ActiveAuras["Arcane Blast"].StackCount == 4 && Me.ManaPercent < 92, "Arcane Barrage"),
                            SC.CastSpell("Arcane Blast", a => true, "Arcane Blast")
                        )
                    ) 
                    #endregion   
                );
            }
        }

        public override Composite Buffs
        {
            get
            {
                return new PrioritySelector(
                    SC.CastBuff("Dalaran Brilliance", a => !SC.PlayerHasBuff("Arcane Brilliance"), "Dalaran Brilliance"),
                    SC.CastBuff("Arcane Brilliance", a => !SC.PlayerHasBuff("Dalaran Brilliance"), "Arcane Brilliance"),
                    SC.CastBuff("Mage Armor", a => true, "Mage Armor")
                );
            }
        }

        public override Composite PVPRotation
        {
            get { throw new NotImplementedException(); }
        }

        public override Composite PVERotation
        {
            get
            {
                return SingleRotation;
            }
        }
    }
}